package com.demo.mycamera;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

    private String TAG = "MainActivity";
    private final static int CAMERA_OK = 10001;
    private static String[] PERMISSIONS_STORAGE = {
        "android.permission.CAMERA",
        "android.permission.WRITE_EXTERNAL_STORAGE" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView);*/
        setContentView(R.layout.activity_main);
        Button btn1 = findViewById(R.id.startCamera1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,Camera1Activity.class));
            }
        });

        Button btn2 = findViewById(R.id.startCamera2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,Camera2Activity.class));
            }
        });
        URLUtil.isNetworkUrl("");
        // 先申请权限
        if (Build.VERSION.SDK_INT > 22 && !checkPermissionAllGranted(PERMISSIONS_STORAGE)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, CAMERA_OK);
        }
    }

    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case CAMERA_OK:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 这里已经获取到了摄像头的权限，想干嘛干嘛了可以
                }else {
                    showWaringDialog();
                }
                break;
            default:
                break;
        }
    }

    private void showWaringDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("警告！")
            .setMessage("请前往设置->应用->PermissionDemo->权限中打开相关权限，否则功能无法正常运行！")
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 一般情况下如果用户不授权的话，功能是无法运行的，做退出处理
                    finish();
                }
            }).show();
    }

    //鼠标点击 改变小球的现实的坐标位置

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /*if(event.getAction() == MotionEvent.ACTION_DOWN){
            Log.d(TAG,"event.X: " + event.getX() + " ,event.Y: " + event.getY());
            mySurfaceView.x = event.getX() - 50;
            mySurfaceView.y = event.getY() - 150;
        }*/

        return true;
    }
}
